package 清洗;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class MapTest extends Mapper<LongWritable, Text, LoggerData, NullWritable> {
    LoggerData k = new LoggerData();
    String source;
    String date;

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] str = value.toString().split(" - - ");
        String[] datas = str[1].split(" ");
        source = datas[2].substring(1) + datas[3] + datas[4].substring(0,datas[4].length()-1);
        if (source.startsWith("GET/static")||source.startsWith("GET/uc_server")){
            return;
        }
        if (source.startsWith("GET/")){
            source = source.substring("GET/".length());
        }else if (source.startsWith("POST/")){
            source = source.substring("POST/".length());
        }
        try {
            date = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.ENGLISH).parse(datas[0].substring(1)))));
            k.set(str[0],date,source);
            context.write(k,NullWritable.get());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
